package net.huashitong.until;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class Until {

    public  static String SmesterDate ="semesterDate";
    public  static String Choosestatue="choosestatue";


    /**
     * httpGet请求
     * @param url
     * @param SocketTimeout
     * @param ConnectTimeout
     * @return
     */
    public static String HttpGET(String url,int SocketTimeout,int ConnectTimeout)
    {
        String response=null;
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet httpget = new HttpGet(url);
        RequestConfig reqConfig = RequestConfig.custom().setSocketTimeout(SocketTimeout).setConnectTimeout(ConnectTimeout).build();
        httpget.setConfig(reqConfig);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
             response  = client.execute(httpget,responseHandler);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     *获取当当前年月日
     */
    public static String getCurrentDate() {

        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String currentDate = sf.format(date);

        return currentDate;
    }

    //获取当前日期时间
    public static String getCurrentDateTime(){
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    //获取年份
    public static String getCurrenYear ()
    {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        return currentYear;
    }


    //属性复制器 add by xxq
    public static void copyPro(Object obj,Object obj2){

        try {
            BeanInfo soursebean = Introspector.getBeanInfo(obj.getClass(), Object.class);
            PropertyDescriptor[] sourpro =soursebean.getPropertyDescriptors();

            BeanInfo soursebean2 = Introspector.getBeanInfo(obj2.getClass(), Object.class);
            PropertyDescriptor[] sourpro2 =soursebean2.getPropertyDescriptors();


            for (int i = 0; i < sourpro.length; i++) {
                for (int j = 0; j < sourpro2.length; j++) {
                    if(sourpro[i].getName().equals(sourpro2[j].getName())&&sourpro[i].getPropertyType().equals(sourpro2[j].getPropertyType())  ){
                        sourpro2[j].getWriteMethod().invoke(obj2, sourpro[i].getReadMethod().invoke(obj));
                        break;
                    }
                }
            }

        } catch (IntrospectionException e) {
            // TODO Auto-generated catch block

        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public  static String  getHalfYear(String coursetime)
    {
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyymmdd");
        String years  =Until.getCurrenYear();
        years=years+"0601";
        Date courseDate =null;
        Date tempyear  = null;
        try {
            tempyear = sdf.parse(years);
            courseDate=sdf.parse(coursetime);

        } catch (ParseException e) {
            e.printStackTrace();
        }


        if(courseDate.compareTo(tempyear)>=0){

            years=Until.getCurrenYear()+"B";
        }else {
            years=Until.getCurrenYear()+"A";
        }
        return years;
    }

    //学期转换
    public static String  ChangeTeam(String team )
    {
        if(StringUtils.isBlank(team))
            return  team;

        if(team.contains("A"))
        {
            team = team.replace("A","年第一学期");
            return  team;
        }
            return team.replace("B", "年第二学期");


    }



}
